import { Component, OnInit } from '@angular/core';
import { Course } from '../../model/course';
import { Student } from '../../model/student';
import { Book } from '../../model/book';
import { Classroom } from '../../model/classroom';
import { Teacher } from '../../model/teacher';
import { CourseResult } from '../../model/courseresult';
import { CourseService } from '../../service/course.service';

@Component({
  selector: 'app-scheduling',
  templateUrl: './scheduling.component.html',
  styleUrls: ['./scheduling.component.css']
})
export class SchedulingComponent implements OnInit {

  c:Course
  cl:Array<Course>
  sl:Array<Student>
  bl:Array<Book>
  rl:Array<Classroom>
  tl:Array<Teacher>
  tosavecourse:CourseResult
  constructor(private courseservice:CourseService) {
    this.tosavecourse = new CourseResult();
    this.c=new Course();
   }

   ngOnInit() {

    this.initCourse();
    this.initStudent();
    this.initBook();
    this.initClassroom();
    this.initTeacher(); 
  }


  initCourse(){
    this.cl = new Array();
    let temparray = new Array()
    for(let i=0 ;i<25;i++){
     let c = new Course()
     c.id= i;
     this.cl.push(c);
    }
    this.courseservice.getcourseList().then((data:any)=>{
      if (data != null ) {
        temparray = data
              for(let i of temparray){
                this.cl[i.cid] = i
              }
       }
    })
   }

   setCourse(searchresult:Array<CourseResult>){
    for(let x of searchresult){
    
      let t = new Course();
      t.classroom= x.classroom
      //t.id=x.cid
      t.name= x.name
      t.student= x.student
      t.teacher= x.teacher
     this.cl[x.cid]=t;
    }
   }
 

  
  initStudent(){
    this.sl=new Array();
    this.courseservice.getStudentList()
    .then((data:any)=>{
      this.sl=data;
    })
  }

  initBook(){
    this.bl = new Array();
   this.courseservice.getBookList()
   .then((data:any)=>{
     this.bl= data;
   })
  }
  
 
  initClassroom(){
    this.rl = new Array();
   this.courseservice.getClassroomList()
   .then((data:any)=>{
     this.rl= data;
   })
  }

  initTeacher(){
    this.tl = new Array();
   this.courseservice.getTeacherList()
   .then((data:any)=>{
     this.tl= data;
   })
  }
 


   onStudentSelect(s:Student){
    console.dir(s);
    this.courseservice.getCourseListByParm('student',s.id)
    .then((data:any)=>{
      console.dir(data);
      this.setCourse(data);
    })
 
   }
 
   onBookSelect(b:Book){
   console.dir(b);
    this.courseservice.getCourseListByParm('book',b.id)
    .then((data:any)=>{
    console.dir(data);
   this.setCourse(data);
    })
   }

   onClassroomSelect(r:Classroom){
     console.dir(r);
     this.courseservice.getCourseListByParm('classroom',r.id)
     .then((data:any)=>{
       console.dir(data);
       this.setCourse(data);
     })
   }

   onTeacherSelect(t:Teacher){
     console.dir(t);
     this.courseservice.getCourseListByParm('teacher',t.id)
     .then((data:any)=>{
       console.dir(data);
       this.setCourse(data);
     })
   }
 
   save(temp: CourseResult){
     this.c.classroom=temp.classroom
     this.c.name = temp.name
      this.c.student = temp.student
      this.c.teacher = temp.teacher
      this.c.cid = temp.cid
      this.c.book = temp.book

      this.courseservice.addCourse(this.c).then((flag:boolean)=>{
        if(flag==true){
          this.c=new Course();
          this.tosavecourse = new CourseResult()
          this.initCourse()
          alert("添加成功！")
        }
        else{
          alert("添加异常，请重试")
        }
      })

    console.dir(this.tosavecourse);

   }


   onGridSelect(cid:number){
    alert("你选中了"+cid)
    this.tosavecourse.cid = cid
   }


   onClassroomValueSelect(r:Classroom){
    this.tosavecourse.classroom=r.name
    this.tosavecourse.name=r.name
  }

   onStudentValueSelect(s:Student){
    this.tosavecourse.student=s.name
    this.tosavecourse.name=s.name
  }

  onTeacherValueSelect(t:Teacher){
    this.tosavecourse.teacher=t.name
    this.tosavecourse.name=t.name
  }

   onBookValueSelect(b:Book){
    this.tosavecourse.book = b.id
    this.tosavecourse.name = b.name
   }

}
